package dao

import (
	"gorm.io/gorm"
	"xlq-demo/app/model"
	"xlq-demo/library/db"
)

type WorkerClientDao struct{}

func NewWorkerClientDao() *WorkerClientDao {
	return new(WorkerClientDao)
}

func (workerClientDao *WorkerClientDao) Create(workerClient *model.WorkerClient) error {
	return db.GetMaster().Create(workerClient).Error
}

func (workerClientDao *WorkerClientDao) Delete(id interface{}) error {
	return db.GetMaster().Where("id=?", id).Delete(new(model.WorkerClient)).Error
}

func (workerClientDao *WorkerClientDao) Update(id string, dataMap map[string]interface{}) error {
	return db.GetMaster().Model(new(model.WorkerClient)).Where("id=?", id).Updates(dataMap).Error
}

func (workerClientDao *WorkerClientDao) Find(id interface{}) (*model.WorkerClient, error) {
	workerClient := new(model.WorkerClient)
	err := db.GetSlave().Where("id=?", id).Take(workerClient).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return workerClient, err
}

func (workerClientDao *WorkerClientDao) Get(ids []interface{}) ([]model.WorkerClient, error) {
	var workerClients []model.WorkerClient
	err := db.GetSlave().Find(&workerClients, ids).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return workerClients, err
}

func (workerClientDao *WorkerClientDao) All() ([]model.WorkerClient, error) {
	var workerClients []model.WorkerClient
	err := db.GetSlave().Find(&workerClients).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return workerClients, err
}

func (workerClientDao *WorkerClientDao) FindByWhere(wheres []string, params []interface{}, orders string) (*model.WorkerClient, error) {
	whereLen := len(wheres)
	workerClient := new(model.WorkerClient)
	db := db.GetSlave()
	for i := 0; i < whereLen; i++ {
		db = db.Where(wheres[i], params[i])
	}
	if orders != "" {
		db = db.Order(orders)
	}
	err := db.Take(&workerClient).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return workerClient, err
}

func (workerClientDao *WorkerClientDao) GetByWhere(wheres []string, params []interface{}, orders string) ([]model.WorkerClient, error) {
	whereLen := len(wheres)
	var workerClients []model.WorkerClient
	db := db.GetSlave()
	for i := 0; i < whereLen; i++ {
		db = db.Where(wheres[i], params[i])
	}
	if orders != "" {
		db = db.Order(orders)
	}
	err := db.Find(&workerClients).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return workerClients, err
}

func (workerClientDao *WorkerClientDao) CountByWhere(wheres []string, params []interface{}) (int64, error) {
	whereLen := len(wheres)
	workerClient := new(model.WorkerClient)
	db := db.GetSlave().Model(workerClient)
	for i := 0; i < whereLen; i++ {
		db = db.Where(wheres[i], params[i])
	}
	var total int64
	err := db.Count(&total).Error
	return total, err
}

func (workerClientDao *WorkerClientDao) ListByWhere(wheres []string, params []interface{}, orders string, offset, limit int) ([]model.WorkerClient, error) {
	var workerClients []model.WorkerClient
	db := db.GetSlave()
	for i := 0; i < len(wheres); i++ {
		db = db.Where(wheres[i], params[i])
	}
	if orders != "" {
		db = db.Order(orders)
	}
	err := db.Offset(offset).Limit(limit).Find(&workerClients).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return workerClients, err
}

func (workerClientDao *WorkerClientDao) Raw(sql string, params []interface{}, out interface{}) error {
	err := db.GetSlave().Raw(sql, params...).Scan(out).Error
	if err == gorm.ErrRecordNotFound {
		err = nil
	}
	return err
}

func (workerClientDao *WorkerClientDao) Exec(sql string, params []interface{}) error {
	return db.GetSlave().Exec(sql, params).Error
}
